//
//  WBStatusPictureView.swift
//  weibo
//
//  Created by yanghong on 15/12/13.
//  Copyright © 2015年 杨鸿. All rights reserved.
//

import UIKit
import SnapKit
let WBStatusPictureViewCellId = "WBStatusPictureViewCellId"

// item 之间的间隔
let itemMargin: CGFloat = 5
//宽度
let  itemWidth = (SCREENWIDTH -  itemMargin * 2 - WBStatusesCellMargin * 2) / 3
class WBStatusPictureView: UICollectionView {
    
    var sizeConstraint: Constraint?
    
    var pic_urls: [WBPicture]? {
        
        didSet{
            
            let size = calculatePicsSize(pic_urls?.count ?? 0)
            
            self.snp_updateConstraints { (make) -> Void in
                
                self.sizeConstraint = make.size.equalTo(size).constraint
            }
            
            
            
            
            reloadData()
        }
        
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        // 注册cell
        registerClass(WBStatusesPictureViewCell.self, forCellWithReuseIdentifier: WBStatusPictureViewCellId)
        dataSource = self
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        layout.minimumLineSpacing = itemMargin
        
        layout.minimumInteritemSpacing = itemMargin
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
    }
    
    // MARK -  计算当前配图控件的大小
    private func calculatePicsSize(count: Int) -> CGSize {
        
        // 先根据图片的张数计算出列数和行数
        // 列数
        let col = count == 4 ? 2 : (count >= 3 ? 3 : count)
        let row = count == 4 ? 2 : ((count - 1) / 3 + 1)
        
        let width =  CGFloat(col)*(itemWidth) + CGFloat(col - 1)*itemMargin
        let height = CGFloat(row)*(itemWidth) + CGFloat(row - 1)*itemMargin
        
        return CGSize(width: width, height: height)
    }
    
}
//数据源
extension WBStatusPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return self.pic_urls?.count ?? 0
        
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WBStatusPictureViewCellId, forIndexPath: indexPath) as! WBStatusesPictureViewCell
        cell.pictureInfo = pic_urls![indexPath.item]
        return cell
    }
    
}
// 自定义collectionViewCell
class WBStatusesPictureViewCell: UICollectionViewCell {
    
    var pictureInfo: WBPicture? {
        
        didSet{
            
            if let urlstring = pictureInfo?.thumbnail_pic {
                
                gifimgView.hidden = !urlstring.hasSuffix(".gif")
                
                picView.sd_setImageWithURL(NSURL(string: urlstring), placeholderImage: UIImage(named: "timeline_image_placeholder") )
            }
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        backgroundColor = RandomColor()
        
        //添加子控件
        addSubview(picView)
        addSubview(gifimgView)
        //添加约束
        // picView
        picView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
            
        }
        
        // gifimgView
        gifimgView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(picView)
            make.trailing.equalTo(picView)
            
        }
        
    }
    
    // MARK -  懒加载
    
    private lazy var picView: UIImageView = {
        
        let imgView = UIImageView()
        
        imgView.clipsToBounds = true
        
        imgView.contentMode = .ScaleAspectFill
        return imgView
    }()
    //gif 标示
    
    private lazy var gifimgView: UIImageView =  UIImageView(image: UIImage(named: "timeline_image_gif"))
}